package com.inspect.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class UniformConfig {
	private static UniformConfig instance = null;
	public static boolean configStatus = false;

	private static String appconfigFilePath = "";

	public static synchronized UniformConfig getInstance() {
		if (instance == null) {
			instance = new UniformConfig();
			instance.init();
		}

		return instance;
	}

	public void init() {
		String appConfigFile = "conf/app.xml";

		String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		System.out.println("current.path: " + path);
		File p = new File(path);

		File ProFile = new File(p.getParent(), appConfigFile);
		try {
			if (ProFile.exists()) {
				appconfigFilePath = ProFile.getCanonicalPath();

				configStatus = true;
			} else {
				System.out.println("can not locate the app configure file in " + ProFile.toURI().toString());
				configStatus = false;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static HashMap<String, String> XMLParser(String elementType, String configName) {
		if ((appconfigFilePath.equals("")) || (!configStatus)) {
			System.out.println("init fail! please investigate");
			return null;
		}

		HashMap hm = new HashMap();
		try {
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(new File(appconfigFilePath));
			Element foo = doc.getRootElement();

			List extList = foo.getChildren(elementType);

			for (Iterator iter = extList.iterator(); iter.hasNext();) {
				List configures = ((Element) iter.next()).getChildren("configures");

				for (Iterator it = configures.iterator(); it.hasNext();) {
					Element configs = (Element) it.next();

					if (!configs.getChildTextTrim("configname").equalsIgnoreCase(configName)) {
						continue;
					}

					List detailList = configs.getChildren("detail");

					for (Iterator it1 = detailList.iterator(); it1.hasNext();) {
						List configList = ((Element) it1.next()).getChildren("config");

						for (Iterator cl = configList.iterator(); cl.hasNext();) {
							Element config = (Element) cl.next();
							hm.put(config.getAttributeValue("name"), config.getAttributeValue("value"));
						}

					}

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return hm;
	}

	public static HashMap<String, String> getCommonConfigUnit(String confignameUnitName) {
		HashMap hm = XMLParser("common", confignameUnitName);

		return hm;
	}

	public static String getCommonConfigName(String confignameUnitName, String configKeyName) {
		HashMap hm = XMLParser("common", confignameUnitName);

		String value = (String) hm.get(configKeyName);
		return value;
	}

	public static HashMap<String, String> getExtConfigUnit(String appname) {
		HashMap hm = XMLParser("ext", appname);
		return hm;
	}

	public static String getExtConfigName(String appname, String configKeyName) {
		HashMap hm = XMLParser("ext", appname);
		String value = (String) hm.get(configKeyName);
		return value;
	}
}
